package com.cook.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cook.entity.Follows;
import com.cook.service.FollowsService;
import com.cook.mapper.FollowsMapper;
import com.cook.vo.user.FollowVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author black-white spider
* @description 针对表【follows(用户关注关系表)】的数据库操作Service实现
* @createDate 2025-10-27 19:54:38
*/
@Service
@Slf4j
public class FollowsServiceImpl extends ServiceImpl<FollowsMapper, Follows> implements FollowsService {

    @Autowired
    private FollowsMapper followsMapper;

    public boolean follow(Long followerId, Long followeeId) {
        log.info("关注: followerId={}, followeeId={}", followerId, followeeId);

        // 参数验证
        if (followerId == null || followerId <= 0) {
            throw new IllegalArgumentException("关注者ID不能为空");
        }
        if (followeeId == null || followeeId <= 0) {
            throw new IllegalArgumentException("被关注者ID不能为空");
        }

        try {
            // 检查关注关系是否存在
            int exists = followsMapper.checkFollowExists(followerId, followeeId);
            if (exists == 1) {
                log.warn("关注关系已存在: followerId={}, followeeId={}", followerId, followeeId);
                return false;
            }

            // 执行关注操作
            int result = followsMapper.addFollow(followerId, followeeId);
            boolean success = result > 0;

            if (success) {
                log.info("关注成功: followerId={}, followeeId={}", followerId, followeeId);
            } else {
                log.warn("关注失败: followerId={}, followeeId={}", followerId, followeeId);
            }

            return success;

        } catch (Exception e) {
            log.error("关注异常: followerId={}, followeeId={}", followerId, followeeId, e);
            throw new RuntimeException("关注失败");
        }
    }

    @Transactional
    public boolean unfollow(Long followerId, Long followeeId) {
        log.info("取消关注: followerId={}, followeeId={}", followerId, followeeId);

        // 参数验证
        if (followerId == null || followerId <= 0) {
            throw new IllegalArgumentException("关注者ID不能为空");
        }
        if (followeeId == null || followeeId <= 0) {
            throw new IllegalArgumentException("被关注者ID不能为空");
        }
        if (followerId.equals(followeeId)) {
            throw new IllegalArgumentException("不能取消关注自己");
        }

        try {
            // 检查关注关系是否存在
            int exists = followsMapper.checkFollowExists(followerId, followeeId);
            if (exists == 0) {
                log.warn("关注关系不存在: followerId={}, followeeId={}", followerId, followeeId);
                return false;
            }

            // 执行取消关注
            int result = followsMapper.deleteFollow(followerId, followeeId);
            boolean success = result > 0;

            if (success) {
                log.info("取消关注成功: followerId={}, followeeId={}", followerId, followeeId);
            } else {
                log.warn("取消关注失败: followerId={}, followeeId={}", followerId, followeeId);
            }

            return success;

        } catch (Exception e) {
            log.error("取消关注异常: followerId={}, followeeId={}", followerId, followeeId, e);
            throw new RuntimeException("取消关注失败");
        }
    }

    public boolean isFollowing(Long followerId, Long followeeId) {
        log.info("检查关注关系: followerId={}, followeeId={}", followerId, followeeId);

        // 参数验证
        if (followerId == null || followerId <= 0) {
            throw new IllegalArgumentException("关注者ID不能为空");
        }
        if (followeeId == null || followeeId <= 0) {
            throw new IllegalArgumentException("被关注者ID不能为空");
        }

        try {
            int exists = followsMapper.checkFollowExists(followerId, followeeId);
            return exists > 0;

        } catch (Exception e) {
            log.error("检查关注关系异常: followerId={}, followeeId={}", followerId, followeeId, e);
            throw new RuntimeException("检查关注关系失败");
        }
    }

    public List<FollowVO> getFollowListByPage(Long followerId, Integer pageNum, Integer pageSize) {
        log.info("分页查询关注列表: followerId={}, pageNum={}, pageSize={}", followerId, pageNum, pageSize);

        try {
            int offset = (pageNum - 1) * pageSize;
            List<FollowVO> followList = followsMapper.selectFollowListByPage(followerId, offset, pageSize);
            log.info("分页查询到 {} 个关注用户", followList.size());
            return followList;

        } catch (Exception e) {
            log.error("分页查询关注列表异常: followerId={}", followerId, e);
            throw new RuntimeException("分页查询关注列表失败");
        }
    }

    public int getFollowCount(Long followerId) {
        log.info("查询关注数量: followerId={}", followerId);


        try {
            int count = followsMapper.selectFollowCount(followerId);
            log.info("用户 {} 的关注数量: {}", followerId, count);
            return count;

        } catch (Exception e) {
            log.error("查询关注数量异常: followerId={}", followerId, e);
            throw new RuntimeException("查询关注数量失败");
        }
    }

    //TODO 修改成分页查询
    public List<FollowVO> getFollowerList(Long followeeId) {
        log.info("查询粉丝列表: followeeId={}", followeeId);

        try {
            List<FollowVO> followerList = followsMapper.selectFollowerList(followeeId);
            log.info("查询到 {} 个粉丝", followerList.size());
            return followerList;

        } catch (Exception e) {
            log.error("查询粉丝列表异常: followeeId={}", followeeId, e);
            throw new RuntimeException("查询粉丝列表失败");
        }
    }

    public int getFollowerCount(Long followeeId) {
        log.info("查询粉丝数量: followeeId={}", followeeId);

        try {
            int count = followsMapper.selectFollowerCount(followeeId);
            log.info("用户 {} 的粉丝数量: {}", followeeId, count);
            return count;

        } catch (Exception e) {
            log.error("查询粉丝数量异常: followeeId={}", followeeId, e);
            throw new RuntimeException("查询粉丝数量失败");
        }
    }
}




